const app = getApp()
const { toUrl, globalData } = getApp();
import { request } from '@/utils/request'
Page({
    toUrl: app.toUrl,
    data: {
        list: [],
        globalData,
        count: 0,
        page: 1,
        field_type: 1,  // field_type 1-楼层，2-类别
        field_value: 0,
        catelist: [], //类别
        storeylist: [] //楼层
    },
    field_value ({ currentTarget }) {
        const { value } = currentTarget.dataset
        this.setData({
            field_value: value
        })
        this.getList(1)
    },
    field_type ({ currentTarget }) {
        const { type } = currentTarget.dataset
        this.setData({
            field_value: 0,
            field_type: type
        })
        this.getList(1)
    },
    clearKeywords () {  this.setData({ page:1, keywords:'' }); this.getList(1) },
    keywords (e) {
        this.setData({
            page:1, keywords:e.detail.value
        })
        this.getList(1)
    },
    onReachBottom(){
        console.log("上拉加载...");
        if (this.data.page <= this.count) {
            this.getList(this.data.page)
        } 
    },
    getList (page = 1) {
        let {  field_type, field_value, list } = this.data
        if (page==1) { list = [] }
        return request('index/leaselist', { data: { page, field_type, field_value } }).then(({data, count}) => {
            this.setData({
                list: list.concat(data),
                page: page + 1,
                count
            })
        })
    },
    onLoad(options) {
        wx.showLoading({
            title: '加载中'
        })
        const { storeylist } = app.globalData
        if (storeylist && storeylist.length>0) {
            this.setData({
                storeylist
            })
        } else {
            request('currency/storeylist').then(({data}) => {
                app.globalData.storeylist = data
                this.setData({ storeylist: data })
            })
        }
        request('currency/catelist', { data: { type: 1 } }).then(({data}) => {
            this.setData({ catelist: data })
        })
        this.getList(1).then(() => {
            setTimeout(() => {
                wx.hideLoading()
            }, 500);
        })
    },


    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})